<template>
  <div class="app-container">
    <!-- 搜索工作栏 -->
    <ContentWrap>
      <el-form
        class="-mb-15px"
        :model="queryParams"
        ref="queryFormRef"
        :inline="true"
      >
        <el-form-item label="跟进内容" prop="followContent">
          <el-input
            v-model="queryParams.followContent"
            placeholder="请输入跟进内容"
            clearable
            @keyup.enter="handleQuery"
            class="!w-240px"
          />
        </el-form-item>
        <el-form-item label="跟进时间" prop="createTime">
          <el-date-picker
            v-model="queryParams.createTime"
            type="daterange"
            value-format="YYYY-MM-DD HH:mm:ss"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
            class="!w-240px"
          />
        </el-form-item>
        <el-form-item>
          <el-button @click="handleQuery"><Icon icon="ep:search" /> 搜索</el-button>
          <el-button @click="resetQuery"><Icon icon="ep:refresh" /> 重置</el-button>
        </el-form-item>
      </el-form>
    </ContentWrap>

    <!-- 列表 -->
    <ContentWrap>
      <div class="mb-10px">
        <el-button
          type="primary"
          @click="openForm('create')"
          v-hasPermi="['oa:customer-follow-up:create']"
        >
          <Icon icon="ep:plus" /> 新增
        </el-button>
      </div>
      <el-table
        v-loading="loading"
        :data="list"
      >
        <el-table-column label="编号" align="center" prop="id" width="100" />
        <el-table-column label="跟进内容" align="center" prop="followContent" :show-overflow-tooltip="true" />
        <el-table-column label="下次跟进时间" align="center" prop="nextFollowTime" width="180">
          <template #default="scope">
            <span>{{ formatDate(scope.row.nextFollowTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="跟进人" align="center" prop="followUserName" width="100" />
        <el-table-column label="创建时间" align="center" prop="createTime" width="180">
          <template #default="scope">
            <span>{{ formatDate(scope.row.createTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" width="150">
          <template #default="scope">
            <el-button
              link
              type="primary"
              @click="openForm('update', scope.row)"
              v-hasPermi="['oa:customer-follow-up:update']"
            >
              编辑
            </el-button>
            <el-button
              link
              type="danger"
              @click="handleDelete(scope.row.id)"
              v-hasPermi="['oa:customer-follow-up:delete']"
            >
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <Pagination
        v-model:total="total"
        v-model:page="queryParams.pageNo"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
      />
    </ContentWrap>

    <!-- 表单弹窗 -->
    <CustomerFollowUpForm ref="formRef" @success="getList" />
  </div>
</template>

<script setup lang="ts">
import { getCustomerFollowUpPage, deleteCustomerFollowUp } from '@/api/oa/customer/followUp'
import CustomerFollowUpForm from '@/views/oa/customerfollowup/CustomerFollowUpForm.vue'
import { formatDate } from '@/utils/formatTime'

defineOptions({ name: 'CustomerFollowUpList' })

const props = defineProps<{
  customerId: number
}>()

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(false) // 列表的加载中
const list = ref([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  customerId: props.customerId,
  followContent: undefined,
  followUserId: undefined,
  createTime: []
})
const queryFormRef = ref() // 表单 Ref
const formRef = ref() // 表单 Ref

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await getCustomerFollowUpPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const openForm = (type: string, data?: any) => {
  formRef.value?.open(type, props.customerId, data?.customerName)
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await deleteCustomerFollowUp(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 初始化 */
onMounted(() => {
  getList()
})

// 监听 customerId 变化
watch(() => props.customerId, (val) => {
  queryParams.customerId = val
  getList()
})
</script> 